package com.wswy.wzcx.ui.other;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.DrawableRes;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.wswy.wzcx.R;
import com.wswy.wzcx.utils.Tools;

/**
 * Created by wangduheng26 on 2018/1/25.
 * wangduheng26@gmail.com
 */

public class DialogFactory {
    public static AlertDialog.Builder create(Context context, String title, String msg, String okText, String cancelText, DialogInterface.OnClickListener okListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(msg)
                .setPositiveButton(okText, okListener)
                .setNegativeButton(cancelText, null);
        return builder;
    }
    public static AlertDialog.Builder create(Context context, int titleId, String msg, String okText, String cancelText, DialogInterface.OnClickListener okListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId)
                .setMessage(msg)
                .setPositiveButton(okText, okListener)
                .setNegativeButton(cancelText, null);
        return builder;
    }
    public static AlertDialog.Builder create(Context context, int titleId, String msg, int okText, int cancelText, DialogInterface.OnClickListener okListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(titleId)
                .setMessage(msg)
                .setPositiveButton(okText, okListener)
                .setNegativeButton(cancelText, null);
        return builder;
    }
    public static AlertDialog.Builder create(Context context, String title, String msg, int okText, int cancelText, DialogInterface.OnClickListener okListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(msg)
                .setPositiveButton(okText, okListener)
                .setNegativeButton(cancelText, null);
        return builder;
    }


    public static AlertDialog.Builder create(Context context, String title, String msg, String okText, DialogInterface.OnClickListener okListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(msg)
                .setPositiveButton(okText, okListener);
        return builder;
    }
    public static AlertDialog.Builder create(Context context, String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title)
                .setMessage(msg);
        return builder;
    }





    public static void showHelp(Context context,@DrawableRes int helpRes){
        Dialog builder = new Dialog(context, R.style.Theme_AppCompat_Light_Dialog_Alert);
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
        builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
//        builder.setOnDismissListener(dialogInterface -> {
//            //nothing;
//        });
        builder.setCancelable(true);
        builder.setCanceledOnTouchOutside(true);
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(helpRes);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = builder.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        Point point = new Point();
        d.getSize(point);
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
//        p.height = (int) (d.getHeight()*0.48 ); // 高度设置为屏幕的0.6
        p.width = (int) (point.x * 0.8); // 宽度设置为屏幕的0.65
        p.height = (int) (point.y * 0.65);
        window.setAttributes(p);
        builder.show();
    }


    public static void showTelHelp(final Context context,final String tel){
        create(context, "客服电话", tel, "立即拨打", "取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Tools.startCallPhone(context,tel.replaceAll("-",""));
            }
        }).show();
    }



    public static DatePickerDialog createDatePickerDialog(Context context,DatePickerDialog.OnDateSetListener listener,int y,int m,int d){
        DatePickerDialog dialog = null;

        try {
            dialog = new DatePickerDialog(context,DatePickerDialog.THEME_HOLO_LIGHT,listener,y,m,d);
        }catch (Throwable throwable){
            throwable.printStackTrace();
            dialog = new DatePickerDialog(context,listener,y,m,d);
        }
        return dialog;
    }

}
